package com.nefu.huiyi.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nefu.huiyi.entity.Doctor;
import com.nefu.huiyi.entity.Material;
import com.nefu.huiyi.result.HttpResult;
import com.nefu.huiyi.service.MaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/materials")
@Tag(name = "必备材料管理接口")
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    @PostMapping("/newMaterial")
    @Operation(summary = "添加材料")
    public HttpResult<Material> addMaterial(@RequestBody Material material) {
        return HttpResult.success(materialService.addMaterial(material));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "根据id删除材料")
    public HttpResult<Boolean> deleteMaterial(@PathVariable Integer id) {
        return materialService.deleteMaterial(id)
                ? HttpResult.success(true)
                : HttpResult.failed("删除失败");
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询材料")
    public HttpResult<Material> getMaterialById(@PathVariable Integer id) {
        log.info("准备查询材料，ID: {}", id); //
        Material material = materialService.getMaterialById(id);
        return HttpResult.success(material);
    }

    @PutMapping
    @Operation(summary = "更新材料")
    public HttpResult<Material> updateMaterial(@RequestBody Material material) {
        Material material1 = materialService.updateMaterial(material);
        return HttpResult.success(material1);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询材料列表")
    public HttpResult<IPage<Material>> getMaterialPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam(required = false) String keyword) {

        Page<Material> page = new Page<>(pageNum, pageSize);
        IPage<Material> materialPage = materialService.getMaterialPage(page, keyword);

        return HttpResult.success(materialPage);
    }
//    @GetMapping("/search")
//    @Operation(summary = "搜索材料")
//    public HttpResult<List<Material>> searchByKeyword(@RequestParam String keyword) {
//        return HttpResult.success(materialService.searchByKeyword(keyword));
//    }
////
//    @GetMapping("/all")
//    @Operation(summary = "获取全部材料")
//
}
